package com.happy.services.security;

import java.util.List;

import javax.management.relation.Role;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.core.orm.hibernate.HibernateDao;
import com.core.orm.hibernate.Page;
import com.core.utils.PropertyFilter;
import com.happy.entity.Authority;
import com.happy.entity.Resource;
import com.happy.entity.User;
import com.happy.exception.ServicesException;

/** 
 * <p>
 * 整个User模块内的业务逻辑Facade类.
 * 使用Spring的@Service/@Autowired 指定IOC设置.
 * 使用Spring的@Transactional指定事务管理.	
 * </p>
 * @author <a href="mailto:happyMpl@163.com">Mpl</a>   
 * @version v 0.1 
*/ 
@Service
@Repository
@Transactional
public class AdminUserManager {

	private static String AUTH_HQL = "select count(u) from user u where u.user_name=? and u.user_password=?";

	private static String RESC_HQL = "from Resource r left join fetch r.authorityList WHERE r.vcResourceType=? ORDER BY r.nuPosition ASC";
	
	private HibernateDao<User, Long> userDao;

	private HibernateDao<Role, Long> roleDao;

	private HibernateDao<Authority, Long> authDao;
	
	private HibernateDao<Resource, Long> resourceDao;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		userDao = new HibernateDao<User, Long>(sessionFactory, User.class);
		roleDao = new HibernateDao<Role, Long>(sessionFactory, Role.class);
		authDao = new HibernateDao<Authority, Long>(sessionFactory, Authority.class);
		resourceDao = new HibernateDao<Resource, Long>(sessionFactory, Resource.class);
	}

	@Transactional(readOnly = true)
	public User getUser(Long id) {
		return userDao.get(id);
	}

	@Transactional(readOnly = true)
	public Page<User> getAllUsers(Page<User> page) {
		return userDao.getAll(page);
	}

	

	public void saveUser(User user) {
		userDao.save(user);
	}

	public void deleteUser(Long id) throws ServicesException {
		if (id == 1)
			throw new ServicesException("不能删除超级用户");
		User user = userDao.get(id);
		userDao.delete(user);
	}
	
	/**
	 * 检查用户名是否唯一.
	 *
	 * @return loginName在数据库中唯一或等于oldLoginName时返回true.
	 */
	@Transactional(readOnly = true)
	public boolean isLoginNameUnique(String loginName, String orgLoginName) {
		return userDao.isPropertyUnique("vcLoginName", loginName, orgLoginName);
	}



	@Transactional(readOnly = true)
	public List<Role> getAllRoles() {
		return roleDao.getAll();
	}

	@Transactional(readOnly = true)
	public Role getRole(Long id) {
		return roleDao.get(id);
	}

	public void saveRole(Role role) {
		roleDao.save(role);
	}

	public void deleteRole(Long id)throws ServicesException {
		if (id == 1)
			throw new ServicesException("不能删除超级管理员角色");
		Role role = roleDao.get(id);
		roleDao.delete(role);
	}

	@Transactional(readOnly = true)
	public List<Authority> getAllAuths() {
		return authDao.getAll();
	}

	@Transactional(readOnly = true)
	public Authority getAuthority(Long id) {
		return authDao.get(id);
	}
	
	/**
	 * 查找URL类型的资源并初始化可访问该资源的授权.
	 */
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true)
	public List<Resource> getUrlResourceWithAuthorities() {
		return resourceDao.createQuery(RESC_HQL, Resource.URL_TYPE).list();
	}

	@Transactional(readOnly = true)
	public List<Authority> getAllAuthority() {
		return authDao.getAll();
	}
}
